/**
 * 站内消息
 */
var _message = function() {
    var self = this;
    self.firstAlert = true;

    // 记录首页弹窗已经弹出的消息ID，避免一直弹
    self.hadAlerts = [];
};
_message.prototype = {};
_message.prototype.init = function() {
    var self = this;
    Util.registerTimer("message", 30, function() {
        self.queryCenter();
        self.queryRight();
        self.queryAlert();
    });
    self.queryAlert();
    self.queryCenter();

    $(".notice-wrap .close-btn").click(function() {
        // 往下翻页
        if ($(".notice-wrap .lay-important").css("display") == 'none') {
            $(".notice-wrap").hide();
        }
        else
            self.pageDown();
    });
    $(".notice-wrap .lay-notice-btn").click(function() {
        // 往下翻页
        self.pageDown();
    });
    $(".notice-wrap .notice-pager .n").click(function() {
        // 往下翻页
        self.pageDown();
    });
    $(".notice-wrap .notice-pager .p").click(function() {
        // 往上翻页
        self.pageUp();
    });
}

_message.prototype.convert = function(data) {
    return Util.convertNotice(data);
}

_message.prototype.queryCenter = function() {
    var self = this;
    CMD.notice_uquery("消息中心", true, 1, 1, function(r) {
        if (r.list.length > 0)
            $("#left_user_new_notice").show();
        else
            $("#left_user_new_notice").hide();
    });
}

_message.prototype.queryRight = function() {
    var self = this;
    CMD.notice_uquery("右下弹窗", true, 1, 1, function(r) {
        if (Util.isEmpty(r.list)) return;
        self.showRight(r.list[0].id, r.list[0].content);
    });
}

_message.prototype.queryAlert = function() {
    var self = this;
    CMD.notice_uquery("首页弹窗", true, 1, 1, function(r) {
        if (Util.isEmpty(r.list)) return;

        var list = [];
        for (var i in r.list) {
            if (self.hadAlerts.indexOf(r.list[i].id) != -1) {
                continue;
            }
            var data = self.convert(r.list[i]);
            if (self.firstAlert != 1 && data.forever) continue;
            list.push(data);
        }

        list.sort(function(a, b) {
            return a.sort - b.sort;
        });
        if (list.length > 0)
            self.alertUI(list);
    });
}

// 弹框显示界面
_message.prototype.alertUI = function(list) {
    var self = this;
    self.alerts = list;
    self.alertIndex = 0;
    $(".notice-wrap").show();
    $(".notice-wrap .lay-important").show();
    $(".notice-wrap .lay-list").hide();
    self.fillAlert();
}

_message.prototype.pageUp = function() {
    var self = this;
    if (self.alertIndex <= 0) return;

    // 当前的消息标记为已读
    if (!self.alerts[self.alertIndex].forever) {
        CMD.notice_flag_read(self.alerts[self.alertIndex].id);
    }
    else {
        self.hadAlerts.push(self.alerts[self.alertIndex].id);
    }
    self.alertIndex--;
    self.fillAlert();
}

_message.prototype.pageDown = function() {
    var self = this;
    if (self.alertIndex >= self.alerts.length - 1) {
        var data = self.alerts[self.alertIndex];
        if (!data.forever) CMD.notice_flag_read(data.id);
        else self.hadAlerts.push(self.alerts[self.alertIndex].id);
        $(".notice-wrap").hide();
        return;
    }

    // 当前的消息标记为已读
    if (!self.alerts[self.alertIndex].forever) {
        CMD.notice_flag_read(self.alerts[self.alertIndex].id);
    }
    else {
        self.hadAlerts.push(self.alerts[self.alertIndex].id);
    }
    self.alertIndex++;
    self.fillAlert();
}

_message.prototype.fillAlert = function() {
    var self = this;
    var list = self.alerts;
    var index = self.alertIndex;
    $(".notice-wrap .lay-important .indicator").html((index + 1) + "/" + list.length);
    $(".notice-wrap .lay-important .notice-content p").html(list[index].content);
}

_message.prototype.showRight = function(id, content) {
    if (!id) {
        $(".bonus-clue .title").html("中奖提示");

        var list = content;
        var content = "";
        for (var i in list) {
            var item = list[i];
            content += get_game_display_name(item.type) + "第" + item.issue + "期 " + Util.convertTarget(item.type, item.target) +
                " 中奖：<span class='red'>" + (item.amount/100).toFixed(2) + "</span><br/>";
        }
        // 延迟关闭
        setTimeout(function() {
            $(".bonus-clue").hide();
        }, 10000);
        Util.playWinMusic();
    }
    else {
        $(".bonus-clue .title").html("站内消息");
        Util.playMessageMusic();
    }
    $(".bonus-clue .c").html(content);
    $(".bonus-clue").show();

    $(".bonus-clue .close").click(function() {
        $(".bonus-clue").hide();
        if (id) {
            // 提示已读
            CMD.notice_flag_read(id);
        }
    });
}

window.Message = new _message();

